<extend name="Public/base"/>
<block name="body">
<!-- 标题 -->
    <div class="main-title">
        <h2>
            邀请码列表页面
        </h2>
    </div>
    </div>
    <!-- 按钮工具栏 -->
    <div class="with-padding">
        <div class="fl">
            <button class="btn ajax-post btn" url="{:U('admin/invite/delete',array('status'=>-1))}" target-form="ids">
                删除
            </button>
            &nbsp;
            <button data-title="生成邀请码" modal-url="{:U('admin/invite/createcode')}" data-role="modal_popup" class="btn">
                生成邀请码
            </button>
            &nbsp;
            <button class="btn ajax-post btn" url="{:U('admin/invite/deletetrue',array('status'=>-1))}" target-form="ids">
                清空无用邀请码(真删除)
            </button>
            &nbsp;
            <button class="btn" data-role="copy_code_list">
                批量复制邀请码
            </button>
            &nbsp;
            <button class="btn"  data-role="copy_code_url_list">
                批量复制邀请链接
            </button>
            &nbsp;
            <button class="btn" data-role="cvs" data-url="{:U('admin/invite/cvs')}">
                导出cvs
            </button>
            &nbsp;
            <!-- 选择框select -->
            <div style="float: right;">
                <style>
                    .oneselect {
                        display: inline-block;
                        margin-left: 10px;
                    }

                    .oneselect .title {
                        float: left;
                        line-height: 32px;
                    }

                    .oneselect .select_box {
                        float: left;
                        line-height: 32px;
                    }

                    .oneselect .select_box select {
                        min-width: 200px;
                    }
                </style>
                <form id="selectForm" method="get" action="{:U('Admin/Invite/invite')}" class="form-dont-clear-url-param">
                    <div class="oneselect">
                        <div class="title">邀请码类型：</div>
                        <div class="select_box">
                            <select name="type" data-role="select_text" class="form-control">
                                <volist name="type_list" id="type">
                                    <php>if($type['id']==$now_type){</php>
                                    <option value="{$type.id}" selected>{$type.value}</option>
                                    <php>}else{</php>
                                    <option value="{$type.id}">{$type.value}</option>
                                    <php>}</php>
                                </volist>
                            </select>
                        </div>
                    </div>
                    <div class="oneselect">
                        <div class="select_box">
                            <select name="status" data-role="select_text" class="form-control">
                                <option value="1" selected>可注册</option>
                                <option value="3">已过期</option>
                                <option value="2">已退还</option>
                                <option value="0">用完无效</option>
                                <option value="-1">管理员删除</option>
                            </select>
                        </div>
                    </div>
                    <div class="oneselect">
                        <div class="select_box">
                            <select name="buyer" data-role="select_text" class="form-control">
                                <option value="-1" <eq name="buyer" value="-1">selected</eq>>管理员生成</option>
                                <option value="1" <eq name="buyer" value="1">selected</eq>>用户购买</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <!-- 数据表格 -->
    <div class="with-padding">
        <table class="table table-bordered table-striped ">
            <!-- 表头 -->
            <thead>
            <tr>
                <th class="row-selected row-selected">
                    <input class="check-all" type="checkbox">
                </th>
                <th>ID</th>
                <th>邀请码</th>
                <th style="width: 250px;">邀请码链接</th>
                <th>邀请码类型</th>
                <th>购买者</th>
                <th>可注册几个</th>
                <th>已注册几个</th>
                <th>有效期至</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
            </thead>

            <!-- 列表 -->
            <tbody>
            <volist name="invite_list" id="invite">
                <tr>
                    <td><input class="ids" type="checkbox" value="{$invite.id}" name="ids[]" data-code="{$invite.code}" data-code-url="{$invite.code_url}"></td>
                    <td>{$invite.id}</td>
                    <td>{$invite.code}</td>
                    <td>{$invite.code_url}</td>
                    <td>{$invite.invite}</td>
                    <td>{$invite.buyer}</td>
                    <td>{$invite.can_num}</td>
                    <td>{$invite.already_num}</td>
                    <td>{$invite.end_time|time_format}</td>
                    <td>{$invite.create_time|time_format}</td>
                    <td>
                        <div style="position: relative;">
                            <a data-role="copy_code" data-code="{$invite.code}">复制邀请码</a> <a data-role="copy_code_url" data-code-url="{$invite.code_url}" style="margin-left: 10px;">复制邀请链接</a>
                        </div>
                    </td>
                </tr>
            </volist>

            </tbody>
        </table>
    </div>
    <!-- 分页 -->
    <div class="with-padding">
        {$pagination}
    </div>
    </div>
</block>
<block name="script">
    <script type="text/javascript" src="__STATIC__/thinkbox/jquery.thinkbox.js"></script>
    <script type="text/javascript" src="__PUBLIC__/js/ext/zclip/jquery.zclip.min.js"></script>
    <script type="text/javascript">
        function toggle_search(){
            $('#search_form').toggle('slide');
        }

        $(document).on('submit', '.form-dont-clear-url-param', function(e){
            e.preventDefault();

            var seperator = "&";
            var form = $(this).serialize();
            var action = $(this).attr('action');
            if(action == ''){
                action = location.href;
            }
            var new_location = action + seperator + form;

            location.href = new_location;

            return false;
        });


    </script>
    <script>
        $(function(){
            $('[data-role="copy_code"]').zclip({
                copy: function () {
                    return $(this).attr('data-code');
                },
                afterCopy: function () {
                    $(this).html('已复制');
                    toast.success('复制成功');
                }
            });
            $('[data-role="copy_code_url"]').zclip({
                copy: function () {
                    return $(this).attr('data-code-url');
                },
                afterCopy: function () {
                    $(this).html('链接已复制');
                    toast.success('复制链接成功');
                }
            });
            $('[data-role="copy_code_list"]').zclip({
                copy: function () {
                    var code_list='';
                    $('.ids').each(function(){
                        if($(this).is(":checked")){
                            code_list+=$(this).attr('data-code')+'\n';
                        }
                    });
                    if(code_list!=''){
                        return code_list;
                    }else{
                        toast.error('请选择数据！');
                    }
                },
                afterCopy: function () {
                    toast.success('批量复制成功');
                }
            });
            $('[data-role="copy_code_url_list"]').zclip({
                copy: function () {
                    var code_list_url='';
                    $('.ids').each(function(){
                        if($(this).is(":checked")){
                            code_list_url+=$(this).attr('data-code-url')+'\n\n';
                        }
                    });
                    if(code_list_url!=''){
                        return code_list_url;
                    }else{
                        toast.error('请选择数据！');
                    }
                },
                afterCopy: function () {
                    toast.success('批量复制成功');
                }
            });
            $('[data-role="cvs"]').click(function(){
                var data_url=$(this).attr('data-url');
                var form=$('.ids');
                if (form.get(0) == undefined) {
                } else if (form.get(0).nodeName == 'FORM') {
                    query = form.serialize();
                } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                    query = form.serialize();
                } else {
                    query = form.find('input,select,textarea').serialize();
                }
                data_url+='&'+query;
                window.open(data_url);
            });
            $('[data-role="select_text"]').change(function(){
                $('#selectForm').submit();
            });
            //模态弹窗
            $('[data-role="modal_popup"]').click(function(){
                var target_url=$(this).attr('modal-url');
                var data_title=$(this).attr('data-title');
                var target_form=$(this).attr('target-form');
                if(target_form!=undefined){
                    //设置了参数时，把参数加入
                    var form=$('.'+target_form);

                    if (form.get(0) == undefined) {
                        updateAlert('没有可操作数据。','danger');
                        return false;
                    } else if (form.get(0).nodeName == 'FORM') {
                        query = form.serialize();
                    } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                        query = form.serialize();
                    } else {
                        query = form.find('input,select,textarea').serialize();
                    }
                    if(!query.length){
                        updateAlert('没有可操作数据。','danger');
                        return false;
                    }
                    target_url=target_url+'&'+query;
                }
                var myModalTrigger = new ModalTrigger({
                    'type':'ajax',
                    'url':target_url,
                    'title':data_title
                });
                myModalTrigger.show();
            });
            $('.tox-confirm').click(function(e){
                var text = $(this).attr('data-confirm');
                var result = confirm(text);
                if(result) {
                    return true;
                } else {
                    e.stopImmediatePropagation();
                    e.stopPropagation();
                    e.preventDefault();
                    return false;
                }
            })
            //导航高亮
            highlight_subnav("{:U('Invite/invite')}");
        });
    </script>
</block>